package top.kaoshanji.q1ch05a;

import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import top.kaoshanji.q1ch05a.dao.SpitterRepository;
import top.kaoshanji.q1ch05a.model.Spitter;
import top.kaoshanji.q1ch05a.web.SpitterController;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/24 16:04
 */
public class SpitterControllerTest {

    /**
     * 代码清单test-q1ch05a-159页a：测试展现表单的控制器方法
     */
    @Test
    public void testShowRegistrationForm() throws Exception {
        SpitterRepository mockRepository = Mockito.mock(SpitterRepository.class);
        SpitterController controller = new SpitterController(mockRepository);

        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        mockMvc.perform(MockMvcRequestBuilders.get("/spitter/showRegistrationForm"))
                .andExpect(MockMvcResultMatchers.view().name("showRegistrationFormPage"));

    }

    /**
     * 代码清单test-q1ch05a-160页a：测试处理表单的控制器方法
     * @throws Exception
     */
    @Test
    public void testProcessRegistration() throws Exception {
        SpitterRepository mockRepository = Mockito.mock(SpitterRepository.class);
        Spitter unsaved = new Spitter("jbauer", "24hours", "Jack", "Bauer", "jbauer@ctu.gov");
        Spitter saved = new Spitter(24L, "jbauer", "24hours", "Jack", "Bauer", "jbauer@ctu.gov");

        Mockito.when(mockRepository.save(unsaved)).thenReturn(saved);

        SpitterController controller = new SpitterController(mockRepository);
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        mockMvc.perform(MockMvcRequestBuilders.post("/spitter/register")
                        .param("firstName", "Jack")
                        .param("lastName", "Bauer")
                        .param("username", "jbauer")
                        .param("password", "24hours")
                        .param("email", "jbauer@ctu.gov"))
                .andExpect(MockMvcResultMatchers.redirectedUrl("/spitter/spitterDetail/jbauer"));

        Mockito.verify(mockRepository, Mockito.atLeastOnce()).save(unsaved);
    }

    /**
     * 代码清单test-q1ch05a-01：测试确保所提交的数据是合法的
     */
    @Test
    public void shouldFailValidationWithNoData() throws Exception {
        SpitterRepository mockRepository = Mockito.mock(SpitterRepository.class);
        SpitterController controller = new SpitterController(mockRepository);
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        mockMvc.perform(MockMvcRequestBuilders.post("/spitter/register"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.view().name("showRegistrationFormPage"))
                .andExpect(MockMvcResultMatchers.model().errorCount(5))
                .andExpect(MockMvcResultMatchers.model().attributeHasFieldErrors(
                        "spitter", "firstName", "lastName", "username", "password", "email"));


    }


}
